package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.InstitutionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Institution;
import com.itheima.service.InstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service(interfaceClass = InstitutionService.class)
@Transactional
public class InstitutionServiceImpl implements InstitutionService {

    @Autowired
    private InstitutionDao institutionDao;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //分页参数传递给分页助手处理
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //通过条件查询
        Page<Institution> page = institutionDao.findByCondition(queryPageBean.getQueryString());
        //返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param institution
     */
    @Override
    public void save(Institution institution) {
        //添加套餐
        institutionDao.save(institution);
        //将图片名称保存到Redis，套餐图片保存在数据库中的图片名称
        Jedis resource = jedisPool.getResource();
        resource.sadd(RedisConstant.INSTITUTION_PIC_DB_RESOURCES, institution.getImg());
        //归还资源
        resource.close();
    }

    /**
     * 根据id查询体检机构
     *
     * @param institutionId
     * @return
     */
    @Override
    public Institution findById(Integer institutionId) {
        return institutionDao.findById(institutionId);
    }

    /**
     * //编辑体检机构
     *
     * @param institution
     */
    @Override
    public void update(Institution institution) {
        //通过id获取原体检机构地址信息
        Institution oldInstitution = institutionDao.findById(institution.getId());
        //判断是否重新上传图片
        if (!oldInstitution.getImg().equals(institution.getImg())) {
            Jedis resource = jedisPool.getResource();
            //如果不相等，则删除redis原体检机构地址图片信息
            resource.srem(RedisConstant.INSTITUTION_PIC_DB_RESOURCES, oldInstitution.getImg());
            //更新体检机构地址信息
            institutionDao.update(institution);
            //将新图片名称信息保存到Redis中
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, institution.getImg());
            //归还资源
            resource.close();
        } else {
            //如果相等，说明图片没有重新上传，直接更新体检机构地址
            institutionDao.update(institution);
        }
    }

    /**
     * 删除体检机构地址信息
     *
     * @param institutionId
     */
    @Override
    public void deleteById(Integer institutionId) {
        //先根据体检机构地址信息id查询是否存在预约设置
        Integer count = institutionDao.findOrderByInstitutionId(institutionId);
        if (count > 0) {
            //如果存在关联关系，给出提示信息
            throw new RuntimeException("删除失败，存在预约体检关联关系");
        }
        //通过id获取体检机构地址信息
        Institution institution = institutionDao.findById(institutionId);
        Jedis resource = jedisPool.getResource();
        //删除redis体检机构地址的图片信息
        resource.srem(RedisConstant.INSTITUTION_PIC_DB_RESOURCES, institution.getImg());
        //归还资源
        resource.close();
        //删除体检机构地址信息
        institutionDao.deleteById(institutionId);
    }
}
